package UnitTest;

import java.util.LinkedHashMap;

/**
 * @Auther: Administrator
 * @Date: 2019/8/24/024 11 15
 * @Description: 最长重复字符串
 */
public class MaxMatchString {

    public static void main(String[] args) {
        String str = "abcdefefvivovivoghijghijk";
        LinkedHashMap<String, Integer> groupMap = new LinkedHashMap<>();
        for (int i = 0; i < str.length(); i++) {
            for (int j = i; j < str.length(); j++) {
                String substring = str.substring(i, j);
                Integer count = calTime(str, substring, 0, 0);
                if (count>1){
                    groupMap.put(substring,count);
                }
            }
        }
        int maxlength = groupMap.keySet().stream().mapToInt(String::length).max().getAsInt();
        String maxKey = groupMap.keySet().stream().filter(key -> key.length() == maxlength).findFirst().get();
        System.out.println(maxKey);
    }

    /**
     * @param str 原始字符串
     * @param substring 子字符串
     * @param index1 当前index位置
     * @param time 已出现次数
     */
    private static Integer calTime(String str, String substring, Integer index1, Integer time) {
        int index = str.indexOf(substring, index1+substring.length());
        if (index > 0 ) {
            time++;
            return    calTime(str, substring, index, time);
        }
        return time;
    }
}
